<?php
/**
 * Data model
 *
 * @category  DataModel
 * @package   DataModel
 * @author    DataModel Generator v1.0
 * @version   $Revision: 127 $
 */

/**
 * Data model for table "coverage_line_status"
 *
 * @category  DataModel
 * @package   DataModel
 * @author    DataModel Generator v1.0
 * @version   $Revision: 127 $
 */
class CoverageLineStatus extends BaseCoverageLineStatus
{
    const NOCODE_LINE = 0;

    const DEADCODE_LINE = 1;

    const UNCOVERED_LINE = 2;

    const COVERED_LINE = 3;

    public static function getProjectCoverageInfo($project_id)
    {
        $project_id = intval($project_id);
        $q = 'SELECT
                    count(`coverage_line_status`.`file_id`) as `count`, 
                    `coverage_line_status`.`status`
               FROM
                    `coverage_line_status`, 
                    `pfile`,
                    `pfolder` 
               WHERE `coverage_line_status`.`file_id` = `pfile`.`id` 
                AND `pfile`.`folder_id` = `pfolder`.`id`
                AND `pfolder`.`project_id` = ' . $project_id . '
               GROUP BY
                    `coverage_line_status`.`status`, 
                    `pfolder`.`project_id`';

        $query = new ORMQuery($q);
        $query->exec();
        $arr = $query->fetchAll();
        return $arr;
    }
    
    public static function getFileCoverageInfo($fileId)
    {
        $q = 'SELECT
                    count(*) as `count`, 
                    `coverage_line_status`.`status`
               FROM
                    `coverage_line_status`
               WHERE `coverage_line_status`.`file_id` = '.$fileId.'
               GROUP BY
                    `coverage_line_status`.`status`';

        $query = new ORMQuery($q);
        $query->exec();
        $arr = $query->fetchAll();
        return $arr;
    }

    public static function getFunctionsCoverafeInfo($fileId,$phpclassId)
    {
        $q = 'SELECT 
            `phpfunction`.id,
            ( 
                SELECT count(*)>1 
                FROM `coverage_line_status` 
                WHERE `coverage_line_status`.`file_id` = '.$fileId.'
                       AND `coverage_line_status`.`line` BETWEEN `phpfunction`.`start_number` 
                       AND `phpfunction`.`end_number`
                       AND `coverage_line_status`.`status` = 3
           ) as `covered`
           FROM `phpfunction`
           WHERE `phpfunction`.`phpclass_id` = '.$phpclassId;
        $query = new ORMQuery($q);
        $query->exec();
        $arr = $query->fetchAll();
        return $arr;
    }

    public static function saveLineInfo(File $file, $num, $info)
    {
        if (!is_integer($num)) {
            throw new InvalidArgumentException(__CLASS__ . '::saveLineInfo - Second argument must be integer: ' . $num);
        }
        $st = new CoverageLineStatus();
        $st->file_id = $file->id;
        switch ($info['count']) {
            case -3:
                $st->status = self::NOCODE_LINE;
                break;
            case -2:
                $st->status = self::DEADCODE_LINE;
                break;
            case -1:
                $st->status = self::UNCOVERED_LINE;
                break;
            default:
                $st->status = self::COVERED_LINE;
        }
        $st->test_count = ($st->status == self::COVERED_LINE) ? $info['count'] : 0;
        $st->line = intval($num);
        $st->save();
        if ($st->test_count > 0) {
            foreach ($info['tests'] as $test) {
                CoverageLineInfo::saveLineInfo($st, $test);
            }
        }
        return $st;
    }


    public static function saveIfNotExists($params = null)
    {
        return parent::saveIfNotExists($params, __CLASS__);
    }
}
